package service.Impl;

import entity.Log;
import service.LogService;
import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;


public class LogServiceImpl implements LogService {
    @Override
    public void recordlog(Log log) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "INSERT INTO log(username, operation_type, operation_time) VALUES (?, ?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, log.getUsername());
            ps.setString(2, log.getOperationType());
            ps.setTimestamp(3, Timestamp.valueOf(log.getOperationDate()));

            int affectedRows = ps.executeUpdate();
            if (affectedRows == 0) {
                throw new SQLException("日志记录失败，影响行数为0");
            }
        } finally {
            JdbcUtils.closeResource(null, ps);
            JdbcUtils.releaseConnection(conn);
        }
    }
}
